using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace MonoFrameWork
{
    public class MiruProgressbar: Microsoft.Xna.Framework.DrawableGameComponent
    {
        public int value;
        public int min;
        public int max;
        public int width;
        public int height;
        public bool visible = true;
        public int sizeBorder = 1;
        public SpriteBatch spriteBatch;
        public Vector2 position;
        public Vector2 velocity;
        public Texture2D texture;
        Texture2D rectangleTexture;

        public Color borderColor = Color.Black;
        public Color backgroundColor = Color.White;
        public Color forColor = Color.Red;
        Game game;

        public MiruProgressbar(Game game,int width,int height,Vector2 position)
            : base(game)
        {
            this.game = game;
            this.value = 50;
            this.min = 0;
            this.max = 100;
            this.width = width;
            this.height = height;
            this.position = position;
            this.velocity = Vector2.Zero;
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            Game.Components.Add(this);
        }
        public MiruProgressbar(Game game, int width, int height, Vector2 position,Color backgroundColor,Color forgroundColor,Color borderColor)
            : base(game)
        {
            this.backgroundColor = backgroundColor;
            this.forColor = forgroundColor;
            this.borderColor = borderColor;
            this.game = game;
            this.value = 50;
            this.min = 0;
            this.max = 100;
            this.width = width;
            this.height = height;
            this.position = position;
            this.velocity = Vector2.Zero;
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            Game.Components.Add(this);
        }
        protected override void LoadContent()
        {
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            position += velocity;
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            if (visible == true)
            {
                spriteBatch.Begin();
                texture = null;
                texture = CreateRectangle(width, height);
                spriteBatch.Draw(texture, position, Color.White);
                spriteBatch.End();
            }
            base.Draw(gameTime);
        }
        private Texture2D CreateRectangle(int width, int height)
        {
            rectangleTexture = null;
      
            rectangleTexture = new Texture2D(game.GraphicsDevice, width, height, false, SurfaceFormat.Color);// create the rectangle texture, ,but it will have no color! lets fix that
            Color[] color = new Color[width * height];//set the color to the amount of pixels in the textures
            for (int i = 0; i < color.Length; i++)//loop through all the colors setting them to whatever values we want
            {
                if (i < width * sizeBorder)
                {
                    color[i] = borderColor;
                }
                else
                    if (i % width <= 0 + sizeBorder || i % width >= width - sizeBorder)
                    {
                        color[i] = borderColor;
                    }
                    else
                        if (i / width >= height - sizeBorder)
                        {
                            color[i] = borderColor;
                        }
                        else
                            if (((i % width) * 100) / width <= (value*100)/this.max)
                            {
                                color[i] = forColor;
                            }
                            else
                                color[i] = backgroundColor;
            }
            rectangleTexture.SetData(color);//set the color data on the texture
            return rectangleTexture;//return the texture
        }
    }
}
